package com.qiaofang.applet.service.property.service.impl;

import com.google.common.collect.Lists;
import com.qiaofang.applet.common.base.MongoBaseService;
import com.qiaofang.applet.common.context.QfContext;
import com.qiaofang.applet.common.context.QfContextHolder;
import com.qiaofang.applet.service.property.domain.Property;
import com.qiaofang.applet.service.property.domain.PropertyContact;
import com.qiaofang.applet.service.property.repository.PropertyContactRepository;
import com.qiaofang.applet.service.property.service.PropertyContactService;
import com.qiaofang.applet.service.property.service.PropertyService;
import com.qiaofang.applet.service.property.service.dto.MyPropertyListDto;
import com.qiaofang.applet.service.property.service.dto.PropertyContactQueryDto;
import com.qiaofang.applet.service.property.service.dto.RentPropertyQueryDto;
import com.qiaofang.applet.service.property.service.dto.SalePropertyQueryDto;
import com.qiaofang.applet.service.property.service.mapstruct.PropertyMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j
@Service
public class PropertyContactServiceImpl extends MongoBaseService<PropertyContact, String> implements PropertyContactService {

    private final PropertyContactRepository propertyContactRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private PropertyService propertyService;

    @Autowired
    private PropertyMapper propertyMapper;

    public PropertyContactServiceImpl(PropertyContactRepository repository) {
        super(repository);
        this.propertyContactRepository = repository;
    }

    @Override
    public PropertyContact findByContactIdAndPropertyUuidAndCompanyUuid(String contactId, String businessId, String companyUuid) {
        return propertyContactRepository.findByContactIdAndPropertyUuidAndCompanyUuid(contactId, businessId, companyUuid);
    }

    @Override
    public List<PropertyContact> findAllByPhone(String phone) {
        return propertyContactRepository.findAllByPhone1AndCorporationCode(phone, QfContextHolder.getContext().getCorporationCode());
    }

    @Override
    public List<MyPropertyListDto> searchMyProperty(PropertyContactQueryDto propertyContactQueryDto) {
        //房源uuid
        List<String> propertyUuids = Optional.ofNullable(findAllByPhone(propertyContactQueryDto.getMemberPhone())).orElseGet(Lists::newArrayList)
                .stream().map(PropertyContact::getPropertyUuid).distinct().collect(Collectors.toList());

        //全部
        if (StringUtils.isBlank(propertyContactQueryDto.getTradeStatus())){
            SalePropertyQueryDto queryDto = new SalePropertyQueryDto();
            queryDto.setPropertyUuids(propertyUuids);
            //租售
            List<Property> salePropertyList = propertyService.findAllMySaleHouseByParams(queryDto);
            RentPropertyQueryDto rentPropertyQueryDto = new RentPropertyQueryDto();
            rentPropertyQueryDto.setPropertyUuids(propertyUuids);
            List<Property> rentPropertyList = propertyService.findAllMyRentHouseByParams(rentPropertyQueryDto);
            List<Property> propertyList = new ArrayList<>();
            propertyList.addAll(salePropertyList);
            propertyList.addAll(rentPropertyList);
            return propertyMapper.domainListToMyPropertyList(propertyList);
        }
        List<Property> propertyListByTradeStatus = propertyService.findAllPropertyByTradeStatus(propertyContactQueryDto.getTradeStatus(), propertyUuids);
        List<MyPropertyListDto> myPropertyListDtoList = propertyMapper.domainListToMyPropertyList(propertyListByTradeStatus);

        Optional.ofNullable(myPropertyListDtoList).orElseGet(Lists::newArrayList)
                .stream().map(property->{
                    if (StringUtils.isNotBlank(property.getTradeType())){
                        property.setTradeType(propertyContactQueryDto.getTradeStatus());
                    }
                    return property;
        }).collect(Collectors.toList());
        return myPropertyListDtoList;
    }
}
